package xx.services.page.rest;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.page.PageList;
import xx.entity.page.PageParam;
import xx.services.Passport;
import xx.services.page.inter.IListInterface;
import xx.services.util.Constants;
import xx.util.string.Format;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * page页面list列表管理服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/page/list")
 * @author Administrator 2010-12-22
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/page/list")
public class ListRest extends Passport implements IListInterface
{

    public ListRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 获取一个list列表，包含列表参数param<br>
     * 
     * @GET
     * @Path ("/{list_id}")
     * @param listId-@PathParam("list_id")
     *            list列表ID
     * @return list列表实体，包含列表参数param
     * @author chenxiang
     */
    @GET
    @Path("/{list_id}")
    public String get(@PathParam("list_id")
    long listId)
    {
        try
        {
            PageList pl = new PageList(qr, owner);
            pl.setListId(listId).load();
            return ok(pl);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取一个list列表html代码<br>
     * 
     * @GET
     * @Path ("/{list_id}/html")
     * @param listId-@PathParam("list_id")
     *            list列表ID
     * @return list列表Html代码，不是json数据
     * @author chenxiang
     */
    @GET
    @Path("/{list_id}/html")
    public String getHtml(@PathParam("list_id")
    long listId)
    {
        try
        {
            PageList pl = new PageList(qr, owner);
            String html = pl.setListId(listId).toHtml();
            return ok(html, HTML);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 获取list对应的参数。<br>
     * 
     * @GET
     * @Path ("/{list_id}/param")
     * @param listId-@PathParam("list_id")
     *            列表ID
     * @return 参数实体
     * @author chenxiang
     */
    @GET
    @Path("/{list_id}/param")
    public String getParam(@PathParam("list_id")
    long listId)
    {
        try
        {
            PageList pl = new PageList(qr, owner);
            PageParam pp = pl.setListId(listId).getParam();
            return ok(pp);
        }
        catch (SQLException e)
        {
            return error(e);
        }
    }

    /**
     * 修改一个list列表。<br>
     * 
     * @PUT
     * @Path ("/{list_id}")
     * @param listId-@PathParam("list_id")
     *            list列表ID
     * @param list-@FormParam("list")
     *            list列表demo的Json字符串，包含有列表参数JSON字符串
     * @return 新的list列表实体
     * @author chenxiang
     */
    @PUT
    @Path("/{list_id}")
    public String update(@PathParam("list_id")
    long listId, @FormParam("list")
    String list, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("templet")
    String templet, @FormParam("clazz")
    String clazz, @FormParam("clazz_id")
    long clazzId, @FormParam("description")
    String description)
    {
        try
        {
            PageList pl = new PageList(qr, owner);
            pl.setListId(listId);
            if (Format.noEmpty(list))
            {
                JsonParser parser = new JsonParser();
                JsonObject jo = parser.parse(list).getAsJsonObject();
                pl.update(jo);
            }
            else
            {
                StringBuilder fields = new StringBuilder();
                if (Format.noEmpty(title))
                {
                    fields.append(PageList.TITLE).append(",");
                    pl.setTitle(title);
                }

                if (Format.noEmpty(container))
                {
                    fields.append(PageList.CONTAINER).append(",");
                    pl.setContainer(container);
                }

                if (Format.noEmpty(templet))
                {
                    fields.append(PageList.TEMPLET).append(",");
                    pl.setTemplet(templet);
                }

                if (Format.noEmpty(clazz))
                {
                    fields.append(PageList.CLAZZ).append(",");
                    pl.setClazz(clazz);
                }

                if (clazzId > 0)
                {
                    fields.append(PageList.CLAZZ_ID).append(",");
                    pl.setClazzId(clazzId);
                }

                if (Format.noEmpty(description))
                {
                    fields.append(PageList.DESCRIPTION).append(",");
                    pl.setDescription(description);
                }

                pl.update(fields.toString());
            }
            return ok(pl);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }
}
